/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProgramUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author TeslaSolari
 */
public class Usertask {

    private boolean logedIn = false;
    private boolean isAdmin = false;
    private String currentUser = null;

    public Usertask() {
    }

    public boolean login(String user, String pass) throws SQLException {
        ResultSet rs = SQLManager.executeQuery("SELECT username,pass FROM users "
                + "WHERE username = '" + user + "' and pass ='" + pass + "'");

        if (rs.next()) {
            logedIn = true;
            isAdmin(user, pass);
            currentUser = user;
        }
        return logedIn;
    }

    public void addUser(String usn, String fn, String sn, String em, String p, String g) {
        SQLManager.insertQuery("INSERT INTO navi_demo (username, name, surname, email, pass, group) "
                + "VALUES (" + usn + ", " + fn + ", " + sn + ", " + em + ", " + p + ", " + g + ")");
    }

    public boolean userExist(String user) throws SQLException {
        boolean exist = false;
        ResultSet rs = SQLManager.executeQuery("SELECT username FROM users WHERE username='" + user + "'");
        if (rs.next()) {
            exist = true;
        }

        return exist;
    }

    public boolean emailExist(String email) throws SQLException {
        boolean exist = false;
        ResultSet rs = SQLManager.executeQuery("SELECT email FROM users WHERE email='" + email + "'");
        if (rs.next()) {
            exist = true;
        }
        return exist;
    }

    public boolean isAdmin(String user, String pass) throws SQLException {

        ResultSet rs = SQLManager.executeQuery("SELECT username,pass FROM users "
                + "WHERE username = '" + user + "' and pass ='" + pass + "'");

        if (rs.next()) {
            rs = SQLManager.executeQuery("SELECT username,type FROM users WHERE username='" + user + "' and type='admin'");
            if (rs.next()) {
                isAdmin = true;
            }
        }
        return isAdmin;
    }

    public boolean isLogedIn() {
        return logedIn;
    }

    public boolean currentUserIsAdmin() {
        return isAdmin;
    }

    
}
